package com.example.freemarker.config;

import com.example.freemarker.FreemarkerConfig;
import com.example.utils.ObjectMaps;
import freemarker.template.Configuration;
import freemarker.template.TemplateModelException;


import java.util.List;

/**
 * @Classname FreeMarkerConfig
 * @Description TODO
 * @Date 2022/3/13 16:14
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class FreeMarkerConfig  {

    Configuration configuration;



    public static void init(List<Class<?>> classList)  {
        try {
            Configuration configuration=(Configuration) ObjectMaps.getObjectByClass(Configuration.class);
            if(configuration==null){
                FreemarkerConfig config=(FreemarkerConfig) ObjectMaps.getObjectByClass(FreemarkerConfig.class);
                if(config!=null){
                    configuration=config.getFreemarkerConfig();
                }
            }
            for (int i = 0; i < classList.size() ; i++) {
                Class< ? > clazz = classList.get(i);
                FreemarkerCompontent freemarkerCompontent=clazz.getAnnotation(FreemarkerCompontent.class);
                if (freemarkerCompontent != null) {
                    String name=freemarkerCompontent.value();
                    Object object=ObjectMaps.getObjectByClass(clazz);
                    configuration.setSharedVariable(name,object);
                }
            }
        }catch (TemplateModelException e){
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    @PostConstruct
//    public void setUp() throws TemplateModelException {
//        configuration.setSharedVariable("strstr", new TestTemplate());
//        configuration.setSharedVariable("note", noteTemplate);
//        configuration.setSharedVariable("GetDate",new GetDate());
//        configuration.setSharedVariable("GetPage",new GetPage());
//        configuration.setSharedVariable("content",contentTemplate);
//        // 根据注解获取bean ,key is bean name ,value is bean object
//        Map<String, Object> map = this.applicationContext.getBeansWithAnnotation(FreemarkerComponent.class);
//        for (String key : map.keySet()) {
//            configuration.setSharedVariable(key, map.get(key));
//        }
//    }


}
